<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.keyword"
              placeholder="商品名称"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="handleAddProduct()" size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="商品图片" width="120" align="center">
          <template slot-scope="scope"
            ><img style="height: 80px" :src="scope.row.imgpath"
          /></template>
        </el-table-column>
        <el-table-column label="商品名称" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.commodityName }}</p>
            <p>品牌：{{ scope.row.brandname }}</p>
          </template>
        </el-table-column>
        <el-table-column label="价格" width="120" align="center">
          <template slot-scope="scope">
            <p>价格：￥{{ scope.row.commodityCurrentPrice }}</p>
          </template>
        </el-table-column>
        <el-table-column label="标签" width="140" align="center">
          <template slot-scope="scope">
            <p>
              上架：
              <el-switch
                @change="handlePublishStatusChange(scope.$index, scope.row)"
                :active-value="true"
                :inactive-value="false"
                v-model="scope.row.isActived"
              >
              </el-switch>
            </p>
            <p>
              推荐：
              <el-switch
                @change="handleRecommendStatusChange(scope.$index, scope.row)"
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.recommandStatus"
              >
              </el-switch>
            </p>
          </template>
        </el-table-column>
        <!-- <el-table-column label="销量" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.sale }}</template>
        </el-table-column> -->
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <p>
              <!-- <el-button
                size="mini"
                @click="handleShowProduct(scope.$index, scope.row)"
                >查看
              </el-button> -->
              <el-button
                size="mini"
                @click="handleUpdateProduct(scope.$index, scope.row)"
                >编辑
              </el-button>
            </p>
            <p>
              <!-- <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除
              </el-button> -->
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        确定
      </el-button>
    </div> -->
    <!-- <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div> -->
    <el-dialog
      title="编辑货品信息"
      :visible.sync="editSkuInfo.dialogVisible"
      width="40%"
    >
      <span>商品货号：</span>
      <span>{{ editSkuInfo.productSn }}</span>
      <el-input
        placeholder="按sku编号搜索"
        v-model="editSkuInfo.keyword"
        size="small"
        style="width: 50%;margin-left: 20px"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="handleSearchEditSku"
        ></el-button>
      </el-input>
      <el-table
        style="width: 100%;margin-top: 20px"
        :data="editSkuInfo.stockList"
        border
      >
        <el-table-column label="SKU编号" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.skuCode"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, index) in editSkuInfo.productAttr"
          :label="item.name"
          :key="item.id"
          align="center"
        >
          <template slot-scope="scope">
            {{ getProductSkuSp(scope.row, index) }}
          </template>
        </el-table-column>
        <el-table-column label="销售价格" width="80" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.price"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="商品库存" width="80" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.stock"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="库存预警值" width="100" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.lowStock"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editSkuInfo.dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSkuConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
const defaultListQuery = {
  keyword: null,
  pageNum: 1,
  pageSize: 5,
  publishStatus: null,
  verifyStatus: null,
  productSn: null,
  productCategoryId: null,
  brandId: null,
};
import { Commodity, commodityQuery } from "../../../api/product";
import { BrandGet } from "../../../api/brand";
import { getShopType } from "../../../api/productAttrCate";
export default {
  name: "productList",
  data() {
    return {
      editSkuInfo: {
        dialogVisible: false,
        productId: null,
        productSn: "",
        productAttributeCategoryId: null,
        stockList: [],
        productAttr: [],
        keyword: null,
      },
      operates: [
        {
          label: "商品上架",
          value: "publishOn",
        },
        {
          label: "商品下架",
          value: "publishOff",
        },
        {
          label: "设为推荐",
          value: "recommendOn",
        },
        {
          label: "取消推荐",
          value: "recommendOff",
        },
        {
          label: "设为新品",
          value: "newOn",
        },
        {
          label: "取消新品",
          value: "newOff",
        },
        {
          label: "转移到分类",
          value: "transferCategory",
        },
        {
          label: "移入回收站",
          value: "recycle",
        },
      ],
      operateType: null,
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: true,
      selectProductCateValue: null,
      multipleSelection: [],
      productCateOptions: [],
      brandOptions: [],
      publishStatusOptions: [
        {
          value: 1,
          label: "上架",
        },
        {
          value: 0,
          label: "下架",
        },
      ],
      verifyStatusOptions: [
        {
          value: 1,
          label: "审核通过",
        },
        {
          value: 0,
          label: "未审核",
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getBrandList();
    this.getProductCateList();
  },
  watch: {
    selectProductCateValue: function(newValue) {
      if (newValue != null && newValue.length == 2) {
        this.listQuery.productCategoryId = newValue[1];
      } else {
        this.listQuery.productCategoryId = null;
      }
    },
  },
  filters: {
    // verifyStatusFilter(value) {
    //   if (value === 1) {
    //     return '审核通过';
    //   } else {
    //     return '未审核';
    //   }
    // }
  },
  methods: {
    getList() {
      this.listLoading = true;
      Commodity().then(({ data }) => {
        var res = data.data;
        res.forEach((e) => {
          var line = e.imgpath.indexOf("files") + 6;
          e.imgpath = "http://localhost:5000/" + e.imgpath.slice(line);
        });
        console.log(res);
        this.list = res;
        this.listLoading = false;
      });
    },
    getBrandList() {
      BrandGet().then(({ data }) => {
        console.log(data);
        var res = data.data;
        res.forEach((e) => {
          var list = {
            label: e.brandName,
            value: e.id,
          };
          this.brandOptions.push(list);
        });
      });
    },
    getProductCateList() {
      getShopType().then(({ data }) => {
        var res = data.data;
        res.forEach((e) => {
          var list = {
            label: e.shopTypeName,
            value: e.id,
          };
          console.log(list);
          this.productCateOptions.push(list);
        });
      });
      //   let list = response.data;
      //   this.productCateOptions = [];
      //   for (let i = 0; i < list.length; i++) {
      //     let children = [];
      //     if (list[i].children != null && list[i].children.length > 0) {
      //       for (let j = 0; j < list[i].children.length; j++) {
      //         children.push({label: list[i].children[j].name, value: list[i].children[j].id});
      //       }
      //     }
      //     this.productCateOptions.push({label: list[i].name, value: list[i].id, children: children});
      //   }
      // });
    },
    handleShowSkuEditDialog(index, row) {
      console.log(row, index);
      // this.editSkuInfo.dialogVisible=true;
      // this.editSkuInfo.productId=row.id;
      // this.editSkuInfo.productSn=row.productSn;
      // this.editSkuInfo.productAttributeCategoryId = row.productAttributeCategoryId;
      // this.editSkuInfo.keyword=null;

      // if(row.productAttributeCategoryId!=null){

      // }
    },
    handleSearchEditSku() {
      // fetchSkuStockList(this.editSkuInfo.productId,{keyword:this.editSkuInfo.keyword}).then(response=>{
      //   this.editSkuInfo.stockList=response.data;
      // });
    },
    handleEditSkuConfirm() {
      // if(this.editSkuInfo.stockList==null||this.editSkuInfo.stockList.length<=0){
      //   this.$message({
      //     message: '暂无sku信息',
      //     type: 'warning',
      //     duration: 1000
      //   });
      //   return
      // }
      // this.$confirm('是否要进行修改', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(()=>{
      //   // updateSkuStockList(this.editSkuInfo.productId,this.editSkuInfo.stockList).then(()=>{
      //   //   this.$message({
      //   //     message: '修改成功',
      //   //     type: 'success',
      //   //     duration: 1000
      //   //   });
      //   //   this.editSkuInfo.dialogVisible=false;
      //   // });
      // });
    },
    handleSearchList() {
      commodityQuery({ queryData: this.listQuery.keyword })
        .then(({ data }) => {
          console.log(data);
          var res = data.data;
          res.forEach((e) => {
            
            e.imgPaht =
              "http://localhost:5000/" +
              e.imgPaht.slice(e.imgPaht.indexOf("files") + 6);
              e.imgpath = e.imgPaht;
          });
          this.list = res;
        })
        .catch();
      // this.listQuery.pageNum = 1;
      // this.getList();
    },
    handleAddProduct() {
      this.$router.push({ path: "/pms/addProduct" });
    },
    handleBatchOperate() {
      // if(this.operateType==null){
      //   this.$message({
      //     message: '请选择操作类型',
      //     type: 'warning',
      //     duration: 1000
      //   });
      //   return;
      // }
      // if(this.multipleSelection==null||this.multipleSelection.length<1){
      //   this.$message({
      //     message: '请选择要操作的商品',
      //     type: 'warning',
      //     duration: 1000
      //   });
      //   return;
      // }
      // this.$confirm('是否要进行该批量操作?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   let ids=[];
      //   for(let i=0;i<this.multipleSelection.length;i++){
      //     ids.push(this.multipleSelection[i].id);
      //   }
      //   switch (this.operateType) {
      //     case this.operates[0].value:
      //       this.updatePublishStatus(1,ids);
      //       break;
      //     case this.operates[1].value:
      //       this.updatePublishStatus(0,ids);
      //       break;
      //     case this.operates[2].value:
      //       this.updateRecommendStatus(1,ids);
      //       break;
      //     case this.operates[3].value:
      //       this.updateRecommendStatus(0,ids);
      //       break;
      //     case this.operates[4].value:
      //       this.updateNewStatus(1,ids);
      //       break;
      //     case this.operates[5].value:
      //       this.updateNewStatus(0,ids);
      //       break;
      //     case this.operates[6].value:
      //       break;
      //     case this.operates[7].value:
      //       this.updateDeleteStatus(1,ids);
      //       break;
      //     default:
      //       break;
      //   }
      //   this.getList();
      // });
    },
    handleSizeChange(val) {
      console.log(val);
      // this.listQuery.pageNum = 1;
      // this.listQuery.pageSize = val;
      // this.getList();
    },
    handleCurrentChange(val) {
      console.log(val);
      // this.listQuery.pageNum = val;
      // this.getList();
    },
    handleSelectionChange(val) {
      console.log(val);
      // this.multipleSelection = val;
    },
    handlePublishStatusChange(index, row) {
      console.log(index, row);
      // let ids = [];
      // ids.push(row.id);
      // this.updatePublishStatus(row.publishStatus, ids);
    },
    handleNewStatusChange(index, row) {
      console.log(index, row);
      // let ids = [];
      // ids.push(row.id);
      // this.updateNewStatus(row.newStatus, ids);
    },
    handleRecommendStatusChange(index, row) {
      console.log(index, row);
      // let ids = [];
      // ids.push(row.id);
      // this.updateRecommendStatus(row.recommandStatus, ids);
    },
    handleResetSearch() {
      this.selectProductCateValue = [];
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
    },
    handleDelete(index, row) {
      console.log(index, row);
      // this.$confirm('是否要进行删除操作?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   let ids = [];
      //   ids.push(row.id);
      //   this.updateDeleteStatus(1,ids);
      // });
    },
    handleUpdateProduct(index, row) {
      console.log(index, row);
      this.$router.push({ path: "/pms/updateProduct", query: { id: row.id } });
    },
    handleShowProduct(index, row) {
      console.log(index, row);
    },
    handleShowVerifyDetail(index, row) {
      console.log(index, row);
      // console.log("handleShowVerifyDetail",row);
    },
    handleShowLog(index, row) {
      console.log(index, row);
      // console.log("handleShowLog",row);
    },
    updatePublishStatus(publishStatus, ids) {
      console.log(publishStatus, ids);
      // let params = new URLSearchParams();
      // params.append('ids', ids);
      // params.append('publishStatus', publishStatus);
      // updatePublishStatus(params).then(() => {
      //   this.$message({
      //     message: '修改成功',
      //     type: 'success',
      //     duration: 1000
      //   });
      // });
    },
    updateNewStatus(newStatus, ids) {
      console.log(newStatus, ids);
      // let params = new URLSearchParams();
      // params.append('ids', ids);
      // params.append('newStatus', newStatus);
      // updateNewStatus(params).then(() => {
      //   this.$message({
      //     message: '修改成功',
      //     type: 'success',
      //     duration: 1000
      //   });
      // });
    },
    updateRecommendStatus(recommendStatus, ids) {
      console.log(recommendStatus, ids);
      // let params = new URLSearchParams();
      // params.append('ids', ids);
      // params.append('recommendStatus', recommendStatus);
      // updateRecommendStatus(params).then(() => {
      //   this.$message({
      //     message: '修改成功',
      //     type: 'success',
      //     duration: 1000
      //   });
      // });
    },
    updateDeleteStatus(deleteStatus, ids) {
      console.log(deleteStatus, ids);
      // let params = new URLSearchParams();
      // params.append('ids', ids);
      // params.append('deleteStatus', deleteStatus);
      // updateDeleteStatus(params).then(() => {
      //   this.$message({
      //     message: '删除成功',
      //     type: 'success',
      //     duration: 1000
      //   });
      // });
      // this.getList();
    },
  },
};
</script>
<style>
.table-container {
  width: 150vh;
  margin-left: 130px;
}
</style>
